///////////////////////////////////////////////////////////////////
// Student name: Jay-Evan Tevis (Instructor)
// Course: COSC 4103 - Computer Graphics
// Assignment: Demonstration
// File name: teapot-driver.cpp
// Purpose: Demonstrates how to render the classic teapot in 
//          an orthographic viewing volume and rotate the teapot
//          independently on each of three axes  
// Limitations: None known
///////////////////////////////////////////////////////////////////

#include <iostream>
#include <GL/glut.h>
#include "../include/Control.h"
#include "../include/Eye.h"
#include "../include/vector3f.h"
#include "../include/Model.h"
#include "../include/ModelPart.h"
#include "../include/Eye.h"
#include "../include/CollisionSphere.h"
using namespace std;

// Constants
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480; 

// Global Variables
int sizeOfTeapot = 6;
bool wireFrameDesired = false;
//Model myModel;
//Model camera;
//Eye myeye;
//Control myControl;
CollisionSphere sphere1;
CollisionSphere sphere2;
vector3f testPoint;

// Function Prototypes
void display(void);
void myReshape(int w, int h);
void createModel();
void update();//test director code, updates model velocity and direction, 
//will also call collision check for physmanager
void generateNewPosition(Model& modelA);//test for velocity based movement

// ######################################################################
int main(int argc, char *argv[])
{

//srand(time(0));

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("3D Teapot (Orthographic)");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutSetCursor(GLUT_CURSOR_NONE);
//glutKeyboardFunc(Control::keyboardHandle);
//glutPassiveMotionFunc(Control::mouseHandle);
createModel();//create test model for control to adjust
glutWarpPointer(320,240);//set pointer at center
//myControl.setModelPtr(&camera);//set control pointer as camera


glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glEnable(GL_TEXTURE_2D); 
glClearColor(0.5, 0.5, 0.5, 1.0); // Gray

glutMainLoop();
return 0;
} // End main


// ######################################################################
void display(void)
{
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

if (wireFrameDesired)
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//update();
//gluLookAt(0,0,40,0,0,-1,0,1,0);

/*
gluLookAt(camera.getPosition().v[0],camera.getPosition().v[1],camera.getPosition().v[2],
camera.getDirection().v[0],camera.getDirection().v[1],camera.getDirection().v[2],
0,1,0);*/

gluLookAt(0,0,0,
0,0,1,
0,1,0);

int test = sphere1.collidesWith(sphere2);
cout<<"Spherecheck: "<<test<<endl;
test = sphere1.collidesWith(testPoint);
cout<<"Pointcheck: "<<test<<endl;

sphere1.display();
sphere2.display();
//myeye.update(camera.getPosition(),camera.getDirection());

/*
//markers used for rotation testing
glPushMatrix();
glColor3f(1,1,1);//white
glTranslatef(0,0,80);//+z marker
glutSolidTeapot(6.0);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(80,0,0);//red +x marker
glutSolidTeapot(6.0);
glPopMatrix();

glPushMatrix();
glColor3f(0,1,0);//green
glTranslatef(0,0,-80);//-z marker
glutSolidTeapot(6.0);
glPopMatrix();

glPushMatrix();
glColor3f(0,0,1);
glTranslatef(-80,0,0);//blue -x marker
glutSolidTeapot(6.0);
glPopMatrix();
*/



glFlush();
glutSwapBuffers();
} // End display


void myReshape(int width, int height)
{
const int DISTANCE = 10000;
const int FOV_ANGLE = 45;

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(FOV_ANGLE,  (float) width / (float) height, 1.0, DISTANCE);

cout << "New window size: " << width << " x " << height << endl;
glMatrixMode(GL_MODELVIEW);
} // End myReshape


void createModel()
{
	float radius = 10;
	vector3f zero(0,0,0);
	
	/*vector3f pos(0,0,0);
	vector3f dir(0,0,0);
	vector3f vel(0,0,0);
	camera.setPosition(pos);
	camera.setDirection(dir);
	camera.setVelocity(vel);*/
	
	vector3f pos3(0,5,30);
	vector3f dir3(0,0,0);
	vector3f vel3(0,0,0);	
	sphere1.setCenterPosition(pos3);
	sphere1.setRelativePosition(zero);
	sphere1.setRadius(radius);

	/*vector3f look(0,0,1);
	myeye.setLookAt(look);//set look to zeros
	myeye.setIsActive(true);
	myeye.update(pos,dir);*/
	
	vector3f pos2(0,0,30);
	vector3f dir2(0,0,0);
	vector3f vel2(0,0,0);
	sphere2.setCenterPosition(pos2);
	sphere2.setRelativePosition(zero);
	sphere2.setRadius(radius);
	
	
	testPoint.v[0]=0;
	testPoint.v[1]=0;
	testPoint.v[2]=38;
}//end function

void update()
{
/*
myControl.updateVelocity();
myControl.updateDirection();
myeye.update(camera.getPosition(),camera.getDirection());
generateNewPosition(camera);
*/



}
/*
void generateNewPosition(Model& modelA)
{//for now it just sets position, ignoring collisions so that we can move the camera
	vector3f tempVel = modelA.getVelocity();
	vector3f tempPos = modelA.getPosition();

	tempPos.v[0] += tempVel.v[0];
	tempPos.v[1] += tempVel.v[1];
	tempPos.v[2] += tempVel.v[2];
	
	modelA.setPosition(tempPos);
	vector3f zeroVect(0,0,0);
	modelA.setVelocity(zeroVect);
}//end gennewPosition
*/